package com.ykq.order.controller;

import com.alibaba.fastjson.JSON;
import com.ykq.Order;
import com.ykq.Product;
import com.ykq.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

//@RestController
//@RequestMapping("order")
@Slf4j
public class OrderController3 {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RestTemplate restTemplate;


    @GetMapping("createOrder/{pid}")
    public String createOrder(@PathVariable("pid")Integer pid, HttpServletRequest request){
        System.out.println(request.getRemoteAddr());
        log.info("下单开始~~~~~~~~~~~~~~~~~~~~~~~~~");
        Order order=new Order();
        order.setUid(1);
        order.setUsername("张三");
        order.setNumber(1);
        log.info("调用远程查询商品接口~~~~~~~~~~~~~~~~");
        Product product = restTemplate.getForObject("http://shop-product/product/findById/" + pid, Product.class);
        //这种习惯 和我们平时编程习惯不同。controller(service对象.findById(id))---service(dao对象.findById)---dao(findById)
        log.info("返回商品信息："+ JSON.toJSONString(product));
        order.setPprice(product.getPprice());
        order.setPname(product.getPname());
        order.setPid(product.getPid());

        orderService.createOrder(order);
        return "下单成功";
    }
}
